package com.dqhl.community.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.adapter.CouponsListAdapter;
import com.dqhl.community.base.BaseFragment;
import com.dqhl.community.model.CouponsUseInfo;
import com.dqhl.community.util.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2016/8/31.
 * 展示未使用、已使用、已过期的优惠券的 fragment
 */
public class CouponsFragment extends BaseFragment {

    private static final String TAG = Constant.COUPONS_UNUSED;
    private TextView tvNoInfo;
    private ListView lvCouponsInfo;
    private List<CouponsUseInfo> couponsUseInfoList = new ArrayList<>();
    private CouponsListAdapter couponsListAdapter;
    private Context context;

    public static Fragment newInstance(String tag) {
        CouponsFragment couponsFragment = new CouponsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TAG, tag);
        couponsFragment.setArguments(bundle);
        return couponsFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_coupons, container, false);
        context = getActivity();
        initView(view);

        return view;
    }

    private void initView(View view) {
        tvNoInfo = (TextView) view.findViewById(R.id.tv_no_info);
        lvCouponsInfo = (ListView) view.findViewById(R.id.lv_coupons_info);

        String tag = getArguments().getString(TAG);
        if (tag == null || tag.length() <= 0) {
            tvNoInfo.setVisibility(View.VISIBLE);
            lvCouponsInfo.setVisibility(View.GONE);
        } else {
            if (tag.equals(Constant.COUPONS_UNUSED)) { //未使用
                couponsUseInfoList.clear();
                for (int i = 1; i <= 10; i++){
                    CouponsUseInfo couponsUseInfo = new CouponsUseInfo();
                    couponsUseInfo.setTime("2016-9-" + i);
                    couponsUseInfo.setShop_name("店铺" + i);
                    couponsUseInfo.setDetails("满100减" + i * 5);
                    couponsUseInfo.setTag("未使用");
                    couponsUseInfoList.add(couponsUseInfo);
                }
                couponsListAdapter = new CouponsListAdapter(context, couponsUseInfoList);
            } else if (tag.equals(Constant.COUPONS_USED)) { //已使用
                couponsUseInfoList.clear();
                for (int i = 1; i < 10; i++){
                    CouponsUseInfo couponsUseInfo = new CouponsUseInfo();
                    couponsUseInfo.setTime("2016-9-" + i);
                    couponsUseInfo.setShop_name("店铺" + i);
                    couponsUseInfo.setDetails("满100减" + i * 5);
                    couponsUseInfo.setTag("已使用");
                    couponsUseInfoList.add(couponsUseInfo);
                }
                couponsListAdapter = new CouponsListAdapter(context, couponsUseInfoList);
            } else if (tag.equals(Constant.COUPONS_OVERDUE)) { //已过期
                couponsUseInfoList.clear();
                for (int i = 1; i < 10; i++){
                    CouponsUseInfo couponsUseInfo = new CouponsUseInfo();
                    couponsUseInfo.setTime("2016-9-" + i);
                    couponsUseInfo.setShop_name("店铺" + i);
                    couponsUseInfo.setDetails("满100减" + i * 5);
                    couponsUseInfo.setTag("已过期");
                    couponsUseInfoList.add(couponsUseInfo);
                }
                couponsListAdapter = new CouponsListAdapter(context, couponsUseInfoList);
            }

            lvCouponsInfo.setAdapter(couponsListAdapter);
        }
    }
}
